package com.neusoft.SPNursingHome.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.neusoft.SPNursingHome.po.ClientCareItem;
import com.neusoft.SPNursingHome.po.ClientCareItemWithCareLevelAndCareItem;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface ClientCareItemDao extends BaseMapper<ClientCareItem> {
    // 定义批量插入方法
    // 使用 @Insert 注解定义批量插入方法
    @Insert("<script>" +
            "INSERT INTO client_care_item (client_care_item_id, service_purchase_date, service_count, service_end_date, care_item_id, care_level_id, client_id, service_status) VALUES " +
            "<foreach collection='list' item='item' index='index' open='' separator=',' close=''>" +
            "(#{item.clientCareItemId}, #{item.servicePurchaseDate}, #{item.serviceCount}, #{item.serviceEndDate}, #{item.careItemId}, #{item.careLevelId}, #{item.clientId}, #{item.serviceStatus})" +
            "</foreach>" +
            "</script>")
    int insertBatch(@Param("list") List<ClientCareItem> list);

    // 删除客户id的客户护理项目（用于客户护理级别被移除时）
    @Delete("DELETE FROM client_care_item WHERE client_id = #{clientId}")
    int deleteClientCareItemsByClientId(@Param("clientId") Integer clientId);


    @Select({
            "SELECT cci.*, cl.care_level_name, ci.care_item_name",
            "FROM client_care_item cci",
            "LEFT JOIN care_level cl ON cci.care_level_id = cl.care_level_id",
            "LEFT JOIN care_item ci ON cci.care_item_id = ci.care_item_id",
            "WHERE cci.client_id = #{clientId}"
    })
    List<ClientCareItemWithCareLevelAndCareItem> getClientCareItemsByClientId(@Param("clientId") int clientId);

    // 更新客户护理项目
    @Update("UPDATE client_care_item SET service_count = #{serviceCount}, service_end_date = #{serviceEndDate} WHERE client_care_item_id = #{clientCareItemId}")
    int updateById(@Param("clientCareItemId") Integer clientCareItemId,
                   @Param("serviceCount") Integer serviceCount,
                   @Param("serviceEndDate") Date serviceEndDate);
}
